﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Adp.Core.Attribute;
using Adp;
using Adp.Common;
using Adp.Core.Services;
using System.Security.Cryptography;
using System.Net;
using Yqd.Enm;
using Yqd.Controllers;
using Yqd.Models;

namespace Yqd.Core
{
    public class RankService
    {


        public static void UpdateUnitRank()
        {
            using (var cx = new YqdContext())
            {
                Logs.Warning<RankService>("部门排行开始更新！");

                var MaxResultTime = cx.Yqd_EvaluateResult.OrderByDescending(it => it.InsTime).FirstOrDefault()?.ResultTime;

                if (MaxResultTime == null)
                {
                    Logs.Warning<RankService>("部门无考核数据！停止更新");
                }
                else
                {
                    var EvaluateResultList = cx.Yqd_EvaluateResult.Where(it => it.ResultTime == MaxResultTime && it.IsShow).ToList();
                    var RankList = cx.Yqd_Rank.Where(it => it.ResultTime == MaxResultTime);
                    var IRL = cx.Yqd_IntegralRule.ToList();
                    var UintList = cx.Yqd_Unit.ToList();

                    var RL = new List<Yqd_Rank>();
                    var WCV = IRL.FirstOrDefault(it => it.IntegralRuleID == Enm.任务状态.已完成).Value;
                    var ZCV = IRL.FirstOrDefault(it => it.IntegralRuleID == Enm.任务状态.正常推进).Value;
                    var YJV = IRL.FirstOrDefault(it => it.IntegralRuleID == Enm.任务状态.预警).Value;
                    var ZHV = IRL.FirstOrDefault(it => it.IntegralRuleID == Enm.任务状态.滞后).Value;
                    foreach (var U in UintList)
                    {
                        var R = RankList.FirstOrDefault(it => it.UnitCode == U.UnitCode);
                        var Count = 0;
                        var WCC = 0;
                        var ZCC = 0;
                        var YJC = 0;
                        var ZHC = 0;

                        var Count_330000 = 0;
                        var WCC_330000 = 0;
                        var ZCC_330000 = 0;
                        var YJC_330000 = 0;
                        var ZHC_330000 = 0;

                        var Count_330700 = 0;
                        var WCC_330700 = 0;
                        var ZCC_330700 = 0;
                        var YJC_330700 = 0;
                        var ZHC_330700 = 0;

                        var Count_330782 = 0;
                        var WCC_330782 = 0;
                        var ZCC_330782 = 0;
                        var YJC_330782 = 0;
                        var ZHC_330782 = 0;

                        if (U.UnitCode == "330782000000")
                        {

                            WCC = EvaluateResultList.Sum(it => it.WC);
                            ZCC = EvaluateResultList.Sum(it => it.ZC);
                            YJC = EvaluateResultList.Sum(it => it.YJ);
                            ZHC = EvaluateResultList.Sum(it => it.ZH);
                            Count = WCC + ZCC + YJC + ZHC;

                            WCC_330000 = EvaluateResultList.Sum(it => it.WC_330000);
                            ZCC_330000 = EvaluateResultList.Sum(it => it.ZC_330000);
                            YJC_330000 = EvaluateResultList.Sum(it => it.YJ_330000);
                            ZHC_330000 = EvaluateResultList.Sum(it => it.ZH_330000);
                            Count_330000 = WCC_330000 + ZCC_330000 + YJC_330000 + ZHC_330000;

                            WCC_330700 = EvaluateResultList.Sum(it => it.WC_330700);
                            ZCC_330700 = EvaluateResultList.Sum(it => it.ZC_330700);
                            YJC_330700 = EvaluateResultList.Sum(it => it.YJ_330700);
                            ZHC_330700 = EvaluateResultList.Sum(it => it.ZH_330700);
                            Count_330700 = WCC_330700 + ZCC_330700 + YJC_330700 + ZHC_330700;

                            WCC_330782 = EvaluateResultList.Sum(it => it.WC_330782);
                            ZCC_330782 = EvaluateResultList.Sum(it => it.ZC_330782);
                            YJC_330782 = EvaluateResultList.Sum(it => it.YJ_330782);
                            ZHC_330782 = EvaluateResultList.Sum(it => it.ZH_330782);
                            Count_330782 = WCC_330782 + ZCC_330782 + YJC_330782 + ZHC_330782;

                        }
                        else
                        {
                            var ER = EvaluateResultList.FirstOrDefault(it => it.LeaderUnitCode == U.UnitCode);
                            Count = ER == null ? 0 : (ER.WC + ER.ZC + ER.YJ + ER.ZH);
                            WCC = ER == null ? 0 : ER.WC;
                            ZCC = ER == null ? 0 : ER.ZC;
                            YJC = ER == null ? 0 : ER.YJ;
                            ZHC = ER == null ? 0 : ER.ZH;

                            Count_330000 = ER == null ? 0 : (ER.WC_330000 + ER.ZC_330000 + ER.YJ_330000 + ER.ZH_330000);
                            WCC_330000 = ER == null ? 0 : ER.WC_330000;
                            ZCC_330000 = ER == null ? 0 : ER.ZC_330000;
                            YJC_330000 = ER == null ? 0 : ER.YJ_330000;
                            ZHC_330000 = ER == null ? 0 : ER.ZH_330000;

                            Count_330700 = ER == null ? 0 : (ER.WC_330700 + ER.ZC_330700 + ER.YJ_330700 + ER.ZH_330700);
                            WCC_330700 = ER == null ? 0 : ER.WC_330700;
                            ZCC_330700 = ER == null ? 0 : ER.ZC_330700;
                            YJC_330700 = ER == null ? 0 : ER.YJ_330700;
                            ZHC_330700 = ER == null ? 0 : ER.ZH_330700;

                            Count_330782 = ER == null ? 0 : (ER.WC_330782 + ER.ZC_330782 + ER.YJ_330782 + ER.ZH_330782);
                            WCC_330782 = ER == null ? 0 : ER.WC_330782;
                            ZCC_330782 = ER == null ? 0 : ER.ZC_330782;
                            YJC_330782 = ER == null ? 0 : ER.YJ_330782;
                            ZHC_330782 = ER == null ? 0 : ER.ZH_330782;

                        }
                        //完成的总积分
                        var WCS = WCC * WCV;
                        //正常的总积分
                        var ZCS = ZCC * ZCV;
                        //预警的总积分
                        var YJS = YJC * YJV;
                        //预警的总积分
                        var ZHS = ZHC * ZHV;
                        //计算平均分
                        var AvgScore = Count == 0 ? 0 : (WCS + ZCS + YJS + ZHS) / Count;
                        var Score = Math.Round(AvgScore, 2);


                        //完成的总积分
                        var WCS_330000 = WCC_330000 * WCV;
                        //正常的总积分
                        var ZCS_330000 = ZCC_330000 * ZCV;
                        //预警的总积分
                        var YJS_330000 = YJC_330000 * YJV;
                        //预警的总积分
                        var ZHS_330000 = ZHC_330000 * ZHV;
                        //计算平均分
                        var AvgScore_330000 = Count_330000 == 0 ? 0 : (WCS_330000 + ZCS_330000 + YJS_330000 + ZHS_330000) / Count_330000;
                        var Score_330000 = Math.Round(AvgScore_330000, 2);


                        //完成的总积分
                        var WCS_330700 = WCC_330700 * WCV;
                        //正常的总积分
                        var ZCS_330700 = ZCC_330700 * ZCV;
                        //预警的总积分
                        var YJS_330700 = YJC_330700 * YJV;
                        //预警的总积分
                        var ZHS_330700 = ZHC_330700 * ZHV;
                        //计算平均分
                        var AvgScore_330700 = Count_330700 == 0 ? 0 : (WCS_330700 + ZCS_330700 + YJS_330700 + ZHS_330700) / Count_330700;
                        var Score_330700 = Math.Round(AvgScore_330700, 2);

                        //完成的总积分
                        var WCS_330782 = WCC_330782 * WCV;
                        //正常的总积分
                        var ZCS_330782 = ZCC_330782 * ZCV;
                        //预警的总积分
                        var YJS_330782 = YJC_330782 * YJV;
                        //预警的总积分
                        var ZHS_330782 = ZHC_330782 * ZHV;
                        //计算平均分
                        var AvgScore_330782 = Count_330782 == 0 ? 0 : (WCS_330782 + ZCS_330782 + YJS_330782 + ZHS_330782) / Count_330782;
                        var Score_330782 = Math.Round(AvgScore_330782, 2);



                        if (R == null)
                        {
                            var model = new Yqd_Rank();
                            model.Count = Count;
                            model.WC = WCC;
                            model.ZC = ZCC;
                            model.YJ = YJC;
                            model.ZH = ZHC;
                            model.Score = Score;

                            model.Count_330000 = Count_330000;
                            model.WC_330000 = WCC_330000;
                            model.ZC_330000 = ZCC_330000;
                            model.YJ_330000 = YJC_330000;
                            model.ZH_330000 = ZHC_330000;
                            model.Score_330000 = Score_330000;

                            model.Count_330700 = Count_330700;
                            model.WC_330700 = WCC_330700;
                            model.ZC_330700 = ZCC_330700;
                            model.YJ_330700 = YJC_330700;
                            model.ZH_330700 = ZHC_330700;
                            model.Score_330700 = Score_330700;

                            model.Count_330782 = Count_330782;
                            model.WC_330782 = WCC_330782;
                            model.ZC_330782 = ZCC_330782;
                            model.YJ_330782 = YJC_330782;
                            model.ZH_330782 = ZHC_330782;
                            model.Score_330782 = Score_330782;

                            model.Name = U.Name;
                            model.UnitCode = U.UnitCode;
                            model.ResultTime = Convert.ToDateTime(MaxResultTime);
                            RL.Add(model);
                        }
                        else
                        {
                            R.WC = WCC;
                            R.ZC = ZCC;
                            R.YJ = YJC;
                            R.ZH = ZHC;
                            R.Score = Score;
                            R.Count = Count;

                            R.WC_330000 = WCC_330000;
                            R.ZC_330000 = ZCC_330000;
                            R.YJ_330000 = YJC_330000;
                            R.ZH_330000 = ZHC_330000;
                            R.Score_330000 = Score_330000;
                            R.Count_330000 = Count_330000;

                            R.WC_330700 = WCC_330700;
                            R.ZC_330700 = ZCC_330700;
                            R.YJ_330700 = YJC_330700;
                            R.ZH_330700 = ZHC_330700;
                            R.Score_330700 = Score_330700;
                            R.Count_330700 = Count_330700;

                            R.WC_330782 = WCC_330782;
                            R.ZC_330782 = ZCC_330782;
                            R.YJ_330782 = YJC_330782;
                            R.ZH_330782 = ZHC_330782;
                            R.Score_330782 = Score_330782;
                            R.Count_330782 = Count_330782;

                            R.ResultTime = Convert.ToDateTime(MaxResultTime);
                        }
                    }
                    if (RL.Count() > 0) { cx.Yqd_Rank.AddRange(RL); }

                    var ret = cx.SaveChanges();

                    var rList = cx.Yqd_Rank.Where(it => it.ResultTime == MaxResultTime && it.UnitCode != "330782000000" && it.Score > 0).ToList();
                    //var i = 1;

                    foreach (var r in rList.OrderByDescending(it => it.Score).ThenByDescending(it => it.Count))
                    {
                        var sc = rList.Count(it => it.Score > r.Score);
                        var px = rList.Where(it => it.Score == r.Score);
                        var pc = px.Count(it => it.Count > r.Count);
                        r.Tactic = (sc + pc) + 1;
                    }
                    //var i_330000 = 1;
                    foreach (var r in rList.OrderByDescending(it => it.Score_330000).ThenByDescending(it => it.Count_330000))
                    {


                        var sc = rList.Count(it => it.Score_330000 > r.Score_330000);
                        var px = rList.Where(it => it.Score_330000 == r.Score_330000);
                        var pc = px.Count(it => it.Count_330000 > r.Count_330000);
                        r.Tactic_330000 = (sc + pc) + 1;


                        //var px = rList.Count(it => it.Score_330000 == r.Score_330000 && it.Count_330000 == r.Count_330000);
                        //if (px > 1)
                        //{
                        //    r.Tactic_330000 = rList.Count(it => it.Score_330000 > r.Score_330000) + 1;
                        //}
                        //else
                        //{
                        //    r.Tactic_330000 = i_330000;
                        //}
                        //i_330000++;
                    }

                    //var i_330700 = 1;
                    foreach (var r in rList.OrderByDescending(it => it.Score_330700).ThenByDescending(it => it.Count_330700))
                    {

                        var sc = rList.Count(it => it.Score_330700 > r.Score_330700);
                        var px = rList.Where(it => it.Score_330700 == r.Score_330700);
                        var pc = px.Count(it => it.Count_330700 > r.Count_330700);
                        r.Tactic_330700 = (sc + pc) + 1;

                        // var px = rList.Count(it => it.Score_330700 == r.Score_330700 && it.Count_330700 == r.Count_330700);
                        // if (px > 1)
                        // {
                        //     r.Tactic_330700 = rList.Count(it => it.Score_330700 > r.Score_330700) + 1;
                        // }
                        // else
                        // {
                        //     r.Tactic_330700 = i_330700;
                        // }
                        //i_330700++;
                    }

                    //var i_330782 = 1;
                    foreach (var r in rList.OrderByDescending(it => it.Score_330782).ThenByDescending(it => it.Count_330782))
                    {
                        var sc = rList.Count(it => it.Score_330782 > r.Score_330782);
                        var px = rList.Where(it => it.Score_330782 == r.Score_330782);
                        var pc = px.Count(it => it.Count_330782 > r.Count_330782);
                        r.Tactic_330782 = (sc + pc) + 1;

                        //var px = rList.Count(it => it.Score_330782 == r.Score_330782 && it.Count_330782 == r.Count_330782);
                        //if (px > 1)
                        //{
                        //    r.Tactic_330782 = rList.Count(it => it.Score_330782 > r.Score_330782) + 1;
                        //}
                        //else
                        //{
                        //    r.Tactic_330782 = i_330782;
                        //}


                        //i_330782++;
                    }

                    cx.SaveChanges();

                    Logs.Warning<RankService>("部门排行开始更新！已更新" + ret + "条。");


                }


            }
        }

        public static void UpQMDataToYiWu()
        {
            using (var cx = new YqdContext())
            {
                Logs.Warning<RankService>("冻结数据对比开始");
                //var Quota = cx.Yqd_Quota.OrderByDescending(it => it.IsShow).ToList();
                var NQuotaList = cx.Yqd_QuotaNew.Where(it => it.IsShow).ToList();
                var NMilestoneList = cx.Yqd_MilestoneNew.Where(it => it.IsShow).ToList();
                var TaskIDs = NQuotaList.Select(it => it.TaskID).ToList();
                var QuotaList = cx.Yqd_Quota.Where(it => TaskIDs.Contains(it.TaskID) && it.IsShow).ToList();
                var MilestoneList = cx.Yqd_Milestone.Where(it => TaskIDs.Contains(it.TaskID) && it.IsShow).ToList();
                var QMList = cx.Yqd_QuotaMsg.ToList();
                var QMsg = new List<Yqd_QuotaMsg>();
                var Qmmaxds = cx.Yqd_QuotaMsg.Where(it => it.IsShow).OrderByDescending(it => it.ds).FirstOrDefault()?.ds ?? 0;
                var NQmaxds = NQuotaList.OrderByDescending(it => it.ds).FirstOrDefault()?.ds ?? 0;
                if (NQmaxds > Qmmaxds)
                {
                    var sql = string.Format("update Yqd_QuotaMsg set IsShow=0");
                    var ret = cx.Database.ExecuteSql(sql);

                    foreach (var NQuota in NQuotaList)
                    {
                        //指标信息
                        var Quota = QuotaList.FirstOrDefault(it => it.QuotaCode == NQuota.QuotaCode);
                        if (Quota != null)
                        {
                            //年度目标值判断
                            if (Quota.TargetValue != NQuota.TargetValue)
                            {
                                var Msg = "更新指标年度目标值（更新年度目标值:" + Quota.TargetValue + "，改为" + NQuota.TargetValue + "）。";
                                QMsg.Add(new Yqd_QuotaMsg
                                {
                                    TargetID = NQuota.QuotaID,
                                    TaskID = NQuota.TaskID,
                                    QuotaCode = NQuota.QuotaCode,
                                    TField = "Yqd_Quota",
                                    TFieldName = "TargetValue",
                                    Msg = Msg,
                                    ds = NQuota.ds,
                                    IsShow = true,
                                    InsTime = DateTime.Now
                                });
                                //var model = new Yqd_EvaluateInfo();
                                //model.TaskStatus = 3;// 3预警
                                //model.TaskID = NQuota.TaskID;
                                //model.TableName = "更新提醒";
                                //model.DataTable = "QuotaMsg";
                                //model.TField = "TargetValue";
                                //model.TFieldName = "年度目标值";
                                //model.TargetID = NQuota.QuotaID;
                                //model.Evaluate = Msg;
                                //model.ds = NQuota.ds;
                                //QE.Add(model);
                            }
                            //当前值判断
                            //if (Quota.CurrentValue != NQuota.CurrentValue)
                            //{
                            //    QMsg.Add(new Yqd_QuotaMsg
                            //    {
                            //        TargetID = NQuota.QuotaID,
                            //        TaskID = NQuota.TaskID,
                            //        QuotaCode = NQuota.QuotaCode,
                            //        TField = "Yqd_Quota",
                            //        TFieldName = "CurrentValue",
                            //        Msg = "更新指标当前值（更新当前值:" + Quota.CurrentValue + "，改为" + NQuota.CurrentValue + "）。",
                            //        ds = NQuota.ds,
                            //        IsShow = true,
                            //        InsTime = DateTime.Now
                            //    });

                            //}
                            //指标值类型
                            //if (Quota.ValueType != NQuota.ValueType)
                            //{
                            //    QMsg.Add(new Yqd_QuotaMsg
                            //    {
                            //        TargetID = NQuota.QuotaID,
                            //        TaskID = NQuota.TaskID,
                            //        QuotaCode = NQuota.QuotaCode,
                            //        TField = "Yqd_Quota",
                            //        TFieldName = "ValueType",
                            //        Msg = "更新指标值类型（更新指标值类型:" + (Quota.ValueType == 1 ? "正向" : "负向") + "，改为" + (NQuota.ValueType == 1 ? "正向" : "负向") + "）。",
                            //        ds = NQuota.ds,
                            //        IsShow = true,
                            //        InsTime = DateTime.Now
                            //    });
                            //}
                            //指标类型
                            //if (Quota.QuotaType != NQuota.QuotaType)
                            //{
                            //    QMsg.Add(new Yqd_QuotaMsg
                            //    {
                            //        TargetID = NQuota.QuotaID,
                            //        TaskID = NQuota.TaskID,
                            //        QuotaCode = NQuota.QuotaCode,
                            //        TField = "Yqd_Quota",
                            //        TFieldName = "QuotaType",
                            //        Msg = "更新指标类型（更新指标类型:" + (Quota.QuotaType == 1 ? "进度类指标" : "监测类指标") + "，改为" + (NQuota.QuotaType == 1 ? "进度类指标" : "监测类指标") + "）。",
                            //        ds = NQuota.ds,
                            //        IsShow = true,
                            //        InsTime = DateTime.Now
                            //    });
                            //}
                            //起始值
                            //if (Quota.StartValue != NQuota.StartValue)
                            //{
                            //    QMsg.Add(new Yqd_QuotaMsg
                            //    {
                            //        TargetID = NQuota.QuotaID,
                            //        TaskID = NQuota.TaskID,
                            //        QuotaCode = NQuota.QuotaCode,
                            //        TField = "Yqd_Quota",
                            //        TFieldName = "StartValue",
                            //        Msg = "更新指标起始值（更新指标起始值:" + Quota.StartValue + "，改为" + Quota.StartValue + "）。",
                            //        ds = NQuota.ds,
                            //        IsShow = true,
                            //        InsTime = DateTime.Now
                            //    });
                            //}

                            ////里程碑信息
                            var Milestone = MilestoneList.Where(it => it.TaskQuotaCode == NQuota.QuotaCode).OrderBy(it => it.MilestoneTime).ToList();
                            var NMilestone = NMilestoneList.Where(it => it.TaskQuotaCode == NQuota.QuotaCode).OrderBy(it => it.MilestoneTime).ToList();
                            var Mc = Milestone.Count();
                            var NMc = NMilestone.Count();
                            var Mlc = NMc > Mc ? NMc : Mc;

                            for (var i = 0; i < Mlc; i++)
                            {

                                var Mi = new Yqd_Milestone();
                                var NMi = new Yqd_MilestoneNew();
                                if ((Mc - 1) >= i)
                                {
                                    Mi = Milestone[i];
                                }

                                if ((NMc - 1) >= i)
                                {
                                    NMi = NMilestone[i];
                                }
                                if (Mi.TaskID == 0 && NMi.TaskID == 0)
                                {
                                    //里程碑数据为空
                                }
                                else if (Mi.TaskID == 0 && NMi.TaskID > 0)
                                {
                                    //冻结数据里程碑为空，最新数据不为空
                                    QMsg.Add(new Yqd_QuotaMsg
                                    {
                                        TargetID = NMi.MilestoneID,
                                        TaskID = NMi.TaskID,
                                        QuotaCode = NMi.TaskQuotaCode,
                                        TField = "Yqd_MilestoneNew",
                                        TFieldName = "MilestoneID",
                                        Msg = "更新:" + NMi.MilestoneTime.ToString("yyyy-MM-dd") + "里程碑节点（添加目标值" + NMi.TargetValue + "）。",
                                        ds = NMi.ds,
                                        IsShow = true,
                                        InsTime = DateTime.Now
                                    });

                                }
                                else if (Mi.TaskID > 0 && NMi.TaskID > 0)
                                {
                                    //冻结数据里程碑不为空，最新数据不为空
                                    if (Mi.MilestoneTime != NMi.MilestoneTime)
                                    {
                                        //节点日期不同
                                        QMsg.Add(new Yqd_QuotaMsg
                                        {
                                            TargetID = NMi.MilestoneID,
                                            TaskID = NMi.TaskID,
                                            QuotaCode = NMi.TaskQuotaCode,
                                            TField = "Yqd_Milestone",
                                            TFieldName = "MilestoneTime",
                                            Msg = "更新:" + Mi.MilestoneTime.ToString("MM月") + "里程碑节点（修改时间节点" + Mi.MilestoneTime.ToString("yyyy-MM-dd") + ",改为" + NMi.MilestoneTime.ToString("yyyy-MM-dd") + "）。",
                                            ds = NMi.ds,
                                            IsShow = true,
                                            InsTime = DateTime.Now
                                        });
                                    }

                                    if (Mi.TargetValue != NMi.TargetValue)
                                    {
                                        //节点目标值不同
                                        QMsg.Add(new Yqd_QuotaMsg
                                        {
                                            TargetID = NMi.MilestoneID,
                                            TaskID = NMi.TaskID,
                                            QuotaCode = NMi.TaskQuotaCode,
                                            TField = "Yqd_Milestone",
                                            TFieldName = "TargetValue",
                                            Msg = "更新:" + Mi.MilestoneTime.ToString("MM月") + "里程碑目标值（修改目标值" + Mi.TargetValue + ",改为" + NMi.TargetValue + "）。",
                                            ds = NMi.ds,
                                            IsShow = true,
                                            InsTime = DateTime.Now
                                        });
                                    }

                                }
                                else if (Mi.TaskID > 0 && NMi.TaskID == 0)
                                {
                                    //冻结数据里程碑不为空，最新数据为空
                                    //冻结数据里程碑为空，最新数据不为空
                                    //QMsg.Add(new Yqd_QuotaMsg
                                    //{
                                    //    TargetID = Mi.MilestoneID,
                                    //    TaskID = Mi.TaskID,
                                    //    QuotaCode = Mi.TaskQuotaCode,
                                    //    TField = "Yqd_MilestoneNew",
                                    //    TFieldName = "MilestoneID",
                                    //    Msg = "更新里程碑节点（删除"+ Mi.MilestoneTime.ToString("MM月") + "节点）",
                                    //    ds = Mi.ds,
                                    //    IsShow = true,
                                    //    InsTime = DateTime.Now
                                    //});
                                }
                            }

                        }

                    }



                    cx.Yqd_QuotaMsg.AddRange(QMsg);
                    var QE = new List<Yqd_EvaluateInfo>();
                    var tl = QMsg.Select(it => it.TargetID).ToList();
                    var EInFo = cx.Yqd_EvaluateInfo.Where(it => it.DataTable == "QuotaMsg").ToList();
                    foreach (var QM in QMsg)
                    {
                        var em = EInFo.FirstOrDefault(it => it.TargetID == QM.TargetID && it.TField == QM.TField && it.TFieldName == QM.TFieldName);
                        if (em == null)
                        {

                            var model = new Yqd_EvaluateInfo();
                            model.TaskStatus = 3;// 3预警
                            model.TaskID = QM.TaskID;
                            model.TableName = "更新预警";
                            model.DataTable = "QuotaMsg";
                            model.TField = QM.TField;
                            model.TFieldName = QM.TFieldName;
                            model.TargetID = QM.TargetID;
                            model.IsShow = true;
                            model.Evaluate = "(" + QuotaList.FirstOrDefault(it => it.QuotaCode == QM.QuotaCode).QuotaName + ")" + QM.Msg;
                            model.ds = QM.ds;
                            model.InsTime = DateTime.Now;
                            QE.Add(model);
                        }
                        else
                        {
                            em.IsShow = true;
                            em.ds = QM.ds;
                        }
                    }
                    //更新预警关联积分规则
                    if (QE.Count > 0)
                    {
                        cx.Yqd_EvaluateInfo.AddRange(QE);

                        var re = EInFo.Where(it => !tl.Contains(it.TargetID));
                        if (re.Count() > 0)
                        {
                            cx.Yqd_EvaluateInfo.RemoveRange(re);
                        }

                    }


                    cx.SaveChanges();
                    Logs.Warning<RankService>("冻结数据对比结束");

                    var delsql = string.Format("DELETE FROM Yqd_QuotaMsg WHERE IsShow = 0");
                    cx.Database.ExecuteSql(delsql);

                    //var upsql = string.Format("update Yqd_Task set TaskStatus=-1 Where TaskID in (SELECT TaskID FROM Yqd_QuotaMsg WHERE IsShow = 1 )");
                    //cx.Database.ExecuteSql(upsql);
                }
                else
                {
                    Logs.Warning<RankService>("NQ:" + NQmaxds + ",QM:" + Qmmaxds + "无最新数据");
                }


            }
        }

        public static void UpQuotaTable()
        {
            using (var cx = new YqdContext())
            {
                Logs.Warning<RankService>("指标清单预警信息更新");
                var qtlist = cx.Yqd_QuotaTable.Where(it => it.TState == 审核状态.同意).Select(it => it.TaskID);
                var em = cx.Yqd_EvaluateInfo.Where(it => it.DataTable == "QuotaTable" && it.IsShow);
                var rem = em.Where(it => qtlist.Contains(it.TaskID) && it.DataTable == "QuotaTable");


                var tasklist = cx.Yqd_Task.Where(it => !qtlist.Contains(it.TaskID));
                var lem = new List<Yqd_EvaluateInfo>();
                foreach (var task in tasklist)
                {
                    //task.TaskStatus = -1;
                    var te = em.FirstOrDefault(it => it.TaskID == task.TaskID);
                    if (te == null)
                    {
                        var model = new Yqd_EvaluateInfo();
                        model.TaskStatus = 3;// 3预警
                        model.TaskID = task.TaskID;
                        model.TableName = "指标清单";
                        model.DataTable = "QuotaTable";
                        model.TField = "TaskID";
                        model.TFieldName = "指标清单未完善";
                        model.TargetID = task.TaskID;
                        model.Evaluate = "指标清单未完善";
                        model.IsShow = true;
                        model.ds = task.ds;
                        model.InsTime = DateTime.Now;
                        lem.Add(model);
                    }
                    else
                    {
                        te.IsShow = true;
                        te.ds = task.ds;
                    }
                }
                cx.Yqd_EvaluateInfo.AddRange(lem);
                cx.Yqd_EvaluateInfo.RemoveRange(rem);
                var ret = cx.SaveChanges();
                Logs.Warning<RankService>("指标清单预警信息更新:" + ret);

            }


        }
        public static void workNotification()
        {
            var dt = DateTime.Now;
            //Logs.Warning<RankService>("推送效能报告：节点{0}。时间{1}", dt.ToString("ddd"), dt.Hour);
            if (dt.ToString("ddd") == "周一" && dt.Hour == 8 && dt.Minute >= 30)
            //if (dt.Hour == 22)
            {
                using (var ctx = new YqdContext())
                {
                    var ddService = Fn.GetService<DdService>();
                    Logs.Warning<RankService>("开始推送效能报告");
                    //todo:年份改下
                    var MaxResultTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
                    var newRankList = ctx.Yqd_Rank.Where(it => it.ResultTime == MaxResultTime).OrderBy(x => x.Tactic).ToList();
                    if (!newRankList.Any())
                    {
                        //var where = "SELECT LeaderUnitCode as UnitCode, LeaderUnitName as UnitName, TaskStatus, COUNT(*) AS Num FROM Yqd_Task WHERE IsShow = 1  and [Year]=" + DateTime.Now.Year;
                        var where = "SELECT LeaderUnitCode as UnitCode, LeaderUnitName as UnitName, TaskStatus, COUNT(*) AS Num FROM Yqd_Task WHERE IsShow = 1  and [Year]=2023";

                        where += " GROUP BY LeaderUnitCode,LeaderUnitName,TaskStatus";
                        var list = ctx.Database.SqlQuery<TaskStatusTJ>(where).ToList();
                        var unit = list.Select(it => new
                        {
                            it.UnitCode,
                            it.UnitName
                        }).Distinct();
                        var IRL = ctx.Yqd_IntegralRule.ToList();
                        var WCV = IRL.FirstOrDefault(it => it.IntegralRuleID == 任务状态.已完成).Value;
                        var ZCV = IRL.FirstOrDefault(it => it.IntegralRuleID == 任务状态.正常推进).Value;
                        var YJV = IRL.FirstOrDefault(it => it.IntegralRuleID == 任务状态.预警).Value;
                        var ZHV = IRL.FirstOrDefault(it => it.IntegralRuleID == 任务状态.滞后).Value;
                        if (unit.Any())
                        {
                            Logs.Warning<RankService>("判断有数据可以推送");
                        }
                        var RankList = new List<Yqd_Rank>();
                        foreach (var m in unit)
                        {
                            var ml = list.Where(it => it.UnitCode == m.UnitCode);
                            var Count = ml.Sum(it => it.Num);
                            var WC = ml.FirstOrDefault(it => it.TaskStatus == (int)任务状态.已完成)?.Num ?? 0;
                            var ZC = ml.FirstOrDefault(it => it.TaskStatus == (int)任务状态.正常推进)?.Num ?? 0;
                            var YJ = ml.FirstOrDefault(it => it.TaskStatus == (int)任务状态.预警)?.Num ?? 0;
                            var ZH = ml.FirstOrDefault(it => it.TaskStatus == (int)任务状态.滞后)?.Num ?? 0;
                            //完成的总积分
                            var WCS = WC * WCV;
                            //正常的总积分
                            var ZCS = ZC * ZCV;
                            //预警的总积分
                            var YJS = YJ * YJV;
                            //预警的总积分
                            var ZHS = ZH * ZHV;
                            //计算平均分
                            var AvgScore = Count == 0 ? 0 : (WCS + ZCS + YJS + ZHS) / Count;
                            var Score = Math.Round(AvgScore, 2);
                            var task = ctx.Yqd_Task.Where(x => x.LeaderUnitCode == m.UnitCode).OrderByDescending(x => x.InsTime).FirstOrDefault();
                            RankList.Add(new Yqd_Rank
                            {
                                RankID = 0,
                                Name = m.UnitName,
                                UnitCode = m.UnitCode,
                                Score = Score,
                                Count = Count,
                                ResultTime = task.InsTime,
                                WC = WC,
                                ZC = ZC,
                                YJ = YJ,
                                ZH = ZH
                            });
                        }
                        var addList = new List<Yqd_Rank>();
                        var result = RankList.OrderByDescending(it => it.Score).ThenByDescending(it => it.Count).ThenByDescending(it => it.ResultTime).Take(250).ToList();
                        Logs.Warning<RankService>("新增排名" + result.Count + "条");
                        foreach (var item in result)
                        {
                            var rank = item;
                            rank.Tactic = result.IndexOf(item) + 1;
                            rank.ResultTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
                            addList.Add(rank);
                        }
                        if (addList.Count() > 0) { ctx.Yqd_Rank.AddRange(addList); }
                        var ret = ctx.SaveChanges();

                        newRankList = ctx.Yqd_Rank.Where(it => it.ResultTime == MaxResultTime).OrderBy(x => x.Tactic).ToList();
                    }
                    var IP = "123.156.65.12";
                    var msglist = new List<Yqd_MsgLog>();
                    foreach (var item in newRankList)
                    {
                        try
                        {
                            var unitList = ctx.Yqd_Unit.Where(it => item.UnitCode == it.UnitCode).ToList();
                            var OC = unitList.Select(it => it.OrganizationCode).ToList();

                            int permit = (int)权限清单.接收效能评价报告;
                            var user = ctx.Yqd_User.Where(it => it.AccountId > 0 && it.PermitItem.Contains(permit.ToString())).ToList();
                            //关闭通知
                            //var msg = ddService.GetNotification_OA(item);
                            var msg = "关闭通知";
                            var ZUC = unitList.FirstOrDefault(it => it.UnitCode == item.UnitCode);
                            var Userlist = user.Where(it => it.UnitCode == ZUC.OrganizationCode).Select(it => it.AccountId);
                            var receiverIds = string.Join(",", Userlist);
                            if (item.UnitCode == "330782000000")
                            {
                                receiverIds = string.Join(",", user.Where(it => it.PermitItem.Contains(permit.ToString())).Select(it => it.AccountId));
                            }
                            Logs.Warning<RankService>($"接收人：{receiverIds}，单位：" + item.Name);
                            //receiverIds = "31028423,79182855,10895460";
                            var model = new Yqd_MsgLog();
                            model.MsgType = 1;
                            model.bizMsgId = item.RankID.ToString();
                            model.receiverIds = receiverIds;
                            model.msg = msg;
                            //model.Result = string.IsNullOrWhiteSpace(receiverIds) ? "接收人为空" : ddService.workNotification("", receiverIds, item.RankID.ToString(), msg, IP);
                            model.Instime = DateTime.Now;
                            msglist.Add(model);
                        }
                        catch (Exception e)
                        {
                            Logs.Warning<RankService>(" 莫名其妙报错了{0},Source:{1},InnerException{2},TargetSite:{3},StackTrace:{4}", e.Message, e.Source, e.InnerException, e.TargetSite, e.StackTrace);
                        }
                    }
                    if (msglist.Count() > 0) { ctx.Yqd_MsgLog.AddRange(msglist); ctx.SaveChanges(); }
                    Logs.Warning<RankService>("部门效能报告推送完成！本次更新" + msglist.Count() + "条");

                    //var MaxResultTime = cx.Yqd_Rank.OrderByDescending(it => it.RankID).FirstOrDefault()?.ResultTime;
                    //var RanKlist = cx.Yqd_Rank.Where(it => it.ResultTime == MaxResultTime && it.Score > 0).ToList();
                    //if (RanKlist.Count() == 0)
                    //{
                    //    Logs.Warning<RankService>("部门无效能报告数据！停止推送");
                    //}
                    //else
                    //{
                    //    Logs.Warning<RankService>("判断有数据可以推送");
                    //    var dtd = dt.Date;
                    //    var mll = cx.Yqd_MsgLog.Where(it => it.MsgType == 1 && it.Instime > dtd).Count();
                    //    if (mll == 0)
                    //    {
                    //        var ddService = Fn.GetService<DdService>();
                    //        var UC = RanKlist.Select(it => it.UnitCode).ToList();
                    //        var unitList = cx.Yqd_Unit.Where(it => UC.Contains(it.UnitCode)).ToList();
                    //        var OC = unitList.Select(it => it.OrganizationCode).ToList();
                    //        var user = cx.Yqd_User.Where(it => OC.Contains(it.UnitCode) && it.AccountId > 0 && (("," + it.PermitItem + ",").Contains("," + (int)权限清单.接收效能评价报告 + ","))).ToList();
                    //        var msglist = new List<Yqd_MsgLog>();
                    //        var IP = "123.156.65.12";
                    //        foreach (var Rank in RanKlist)
                    //        {
                    //            try
                    //            {
                    //                var msg = ddService.GetNotification_OA(Rank);
                    //                var ZUC = unitList.FirstOrDefault(it => it.UnitCode == Rank.UnitCode);
                    //                var Userlist = user.Where(it => it.UnitCode == ZUC.OrganizationCode).Select(it => it.AccountId);
                    //                var receiverIds = string.Join(",", Userlist);
                    //                if (Rank.UnitCode == "330782000000")
                    //                {
                    //                    receiverIds = string.Join(",", user.Where(it => ("," + it.PermitItem + ",").Contains("," + (int)权限清单.接收效能评价报告 + ",")).Select(it => it.AccountId));
                    //                }
                    //                var model = new Yqd_MsgLog();
                    //                model.MsgType = 1;
                    //                model.bizMsgId = Rank.RankID.ToString();
                    //                model.receiverIds = receiverIds;
                    //                model.msg = msg;
                    //                model.Result = string.IsNullOrWhiteSpace(receiverIds) ? "接收人为空" : ddService.workNotification("", receiverIds, Rank.RankID.ToString(), msg, IP);
                    //                model.Instime = DateTime.Now;
                    //                msglist.Add(model);
                    //            }
                    //            catch (Exception e)
                    //            {
                    //                Logs.Warning<RankService>(" 莫名其妙报错了{0},Source:{1},InnerException{2},TargetSite:{3},StackTrace:{4}", e.Message, e.Source, e.InnerException, e.TargetSite, e.StackTrace);
                    //            }

                    //        }
                    //        if (msglist.Count() > 0) { cx.Yqd_MsgLog.AddRange(msglist); }
                    //        Logs.Warning<RankService>("部门效能报告推送完成！本次更新" + msglist.Count() + "条");
                }
            }




}

public static void UpdateUnitNewRank()
{
    using (var ctx = new YqdContext())
    {
        Logs.Warning<RankService>("部门效能开始更新！");
        var year = DateTime.Now.Year;
        var task = ctx.Yqd_Task.Where(it => it.Year == year + "" && it.IsShow).Select(it => new { it.LeaderUnitCode, it.LeaderUnitName, it.TaskStatus, it.TaskType, it.FAreaCode }).ToList();
        var IRL = ctx.Yqd_IntegralRule.ToList();
        var WCV = IRL.FirstOrDefault(it => it.IntegralRuleID == 任务状态.已完成).Value;
        var ZCV = IRL.FirstOrDefault(it => it.IntegralRuleID == 任务状态.正常推进).Value;
        var YJV = IRL.FirstOrDefault(it => it.IntegralRuleID == 任务状态.预警).Value;
        var ZHV = IRL.FirstOrDefault(it => it.IntegralRuleID == 任务状态.滞后).Value;
        var Count = task.Count();
        var rank = new List<Yqd_NewRank>();
        //全部
        var WC = task.Count(it => it.TaskStatus == (int)任务状态.已完成);
        var ZC = task.Count(it => it.TaskStatus == (int)任务状态.正常推进);
        var YJ = task.Count(it => it.TaskStatus == (int)任务状态.预警);
        var ZH = task.Count(it => it.TaskStatus == (int)任务状态.滞后);
        var WCS = WC * WCV;
        var ZCS = ZC * ZCV;
        var YJS = YJ * YJV;
        var ZHS = ZH * ZHV;
        var AvgScore = Count == 0 ? 0 : (WCS + ZCS + YJS + ZHS) / Count;
        var Score = Math.Round(AvgScore, 2);
        rank.Add(new Yqd_NewRank()
        {
            Year = year,
            Name = "全部",
            UnitCode = "",
            FAreaCode = "",
            TaskType = "",
            Score = Score,
            ResultTime = DateTime.Now.Date,
            Tactic = ctx.Database.MaxID("Yqd_NewRank", "Tactic")
        });
        //政府工作报告
        var tt = task.Where(it => ("," + it.TaskType + ",").Contains(",1,"));
        Count = tt.Count();
        WC = tt.Count(it => it.TaskStatus == (int)任务状态.已完成);
        ZC = tt.Count(it => it.TaskStatus == (int)任务状态.正常推进);
        YJ = tt.Count(it => it.TaskStatus == (int)任务状态.预警);
        ZH = tt.Count(it => it.TaskStatus == (int)任务状态.滞后);
        WCS = WC * WCV;
        ZCS = ZC * ZCV;
        YJS = YJ * YJV;
        ZHS = ZH * ZHV;
        AvgScore = Count == 0 ? 0 : (WCS + ZCS + YJS + ZHS) / Count;
        Score = Math.Round(AvgScore, 2);
        rank.Add(new Yqd_NewRank()
        {
            Year = year,
            Name = "全部",
            UnitCode = "",
            FAreaCode = "",
            TaskType = "1",
            Score = Score,
            ResultTime = DateTime.Now.Date,
            Tactic = ctx.Database.MaxID("Yqd_NewRank", "Tactic")
        });
        //政府工作报告征求意见对接会
        var ts = task.Where(it => ("," + it.TaskType + ",").Contains(",2,"));
        Count = ts.Count();
        WC = ts.Count(it => it.TaskStatus == (int)任务状态.已完成);
        ZC = ts.Count(it => it.TaskStatus == (int)任务状态.正常推进);
        YJ = ts.Count(it => it.TaskStatus == (int)任务状态.预警);
        ZH = ts.Count(it => it.TaskStatus == (int)任务状态.滞后);
        WCS = WC * WCV;
        ZCS = ZC * ZCV;
        YJS = YJ * YJV;
        ZHS = ZH * ZHV;
        AvgScore = Count == 0 ? 0 : (WCS + ZCS + YJS + ZHS) / Count;
        Score = Math.Round(AvgScore, 2);
        rank.Add(new Yqd_NewRank()
        {
            Year = year,
            Name = "全部",
            UnitCode = "",
            FAreaCode = "",
            TaskType = "2",
            Score = Score,
            ResultTime = DateTime.Now.Date,
            Tactic = ctx.Database.MaxID("Yqd_NewRank", "Tactic")
        });
        //浙江省
        var t = task.Where(it => it.FAreaCode.Trim() == "330000000000");
        Count = t.Count();
        //全部
        WC = t.Count(it => it.TaskStatus == (int)任务状态.已完成);
        ZC = t.Count(it => it.TaskStatus == (int)任务状态.正常推进);
        YJ = t.Count(it => it.TaskStatus == (int)任务状态.预警);
        ZH = t.Count(it => it.TaskStatus == (int)任务状态.滞后);
        WCS = WC * WCV;
        ZCS = ZC * ZCV;
        YJS = YJ * YJV;
        ZHS = ZH * ZHV;
        AvgScore = Count == 0 ? 0 : (WCS + ZCS + YJS + ZHS) / Count;
        Score = Math.Round(AvgScore, 2);
        rank.Add(new Yqd_NewRank()
        {
            Year = year,
            Name = "浙江省",
            UnitCode = "330000000000",
            FAreaCode = "330000000000",
            TaskType = "",
            Score = Score,
            ResultTime = DateTime.Now.Date,
            Tactic = ctx.Database.MaxID("Yqd_NewRank", "Tactic")
        });
        //政府工作报告
        tt = t.Where(it => ("," + it.TaskType + ",").Contains(",1,"));
        Count = tt.Count();
        WC = tt.Count(it => it.TaskStatus == (int)任务状态.已完成);
        ZC = tt.Count(it => it.TaskStatus == (int)任务状态.正常推进);
        YJ = tt.Count(it => it.TaskStatus == (int)任务状态.预警);
        ZH = tt.Count(it => it.TaskStatus == (int)任务状态.滞后);
        WCS = WC * WCV;
        ZCS = ZC * ZCV;
        YJS = YJ * YJV;
        ZHS = ZH * ZHV;
        AvgScore = Count == 0 ? 0 : (WCS + ZCS + YJS + ZHS) / Count;
        Score = Math.Round(AvgScore, 2);
        rank.Add(new Yqd_NewRank()
        {
            Year = year,
            Name = "浙江省",
            UnitCode = "330000000000",
            FAreaCode = "330000000000",
            TaskType = "1",
            Score = Score,
            ResultTime = DateTime.Now.Date,
            Tactic = ctx.Database.MaxID("Yqd_NewRank", "Tactic")
        });
        //政府工作报告征求意见对接会
        ts = t.Where(it => ("," + it.TaskType + ",").Contains(",2,"));
        Count = ts.Count();
        WC = ts.Count(it => it.TaskStatus == (int)任务状态.已完成);
        ZC = ts.Count(it => it.TaskStatus == (int)任务状态.正常推进);
        YJ = ts.Count(it => it.TaskStatus == (int)任务状态.预警);
        ZH = ts.Count(it => it.TaskStatus == (int)任务状态.滞后);
        WCS = WC * WCV;
        ZCS = ZC * ZCV;
        YJS = YJ * YJV;
        ZHS = ZH * ZHV;
        AvgScore = Count == 0 ? 0 : (WCS + ZCS + YJS + ZHS) / Count;
        Score = Math.Round(AvgScore, 2);
        rank.Add(new Yqd_NewRank()
        {
            Year = year,
            Name = "浙江省",
            UnitCode = "330000000000",
            FAreaCode = "330000000000",
            TaskType = "2",
            Score = Score,
            ResultTime = DateTime.Now.Date,
            Tactic = ctx.Database.MaxID("Yqd_NewRank", "Tactic")
        });

        //金华市
        t = task.Where(it => it.FAreaCode.Trim() == "330700000000");
        Count = t.Count();
        //全部
        WC = t.Count(it => it.TaskStatus == (int)任务状态.已完成);
        ZC = t.Count(it => it.TaskStatus == (int)任务状态.正常推进);
        YJ = t.Count(it => it.TaskStatus == (int)任务状态.预警);
        ZH = t.Count(it => it.TaskStatus == (int)任务状态.滞后);
        WCS = WC * WCV;
        ZCS = ZC * ZCV;
        YJS = YJ * YJV;
        ZHS = ZH * ZHV;
        AvgScore = Count == 0 ? 0 : (WCS + ZCS + YJS + ZHS) / Count;
        Score = Math.Round(AvgScore, 2);
        rank.Add(new Yqd_NewRank()
        {
            Year = year,
            Name = "金华市",
            UnitCode = "330700000000",
            FAreaCode = "330700000000",
            TaskType = "",
            Score = Score,
            ResultTime = DateTime.Now.Date,
            Tactic = ctx.Database.MaxID("Yqd_NewRank", "Tactic")
        });
        //政府工作报告
        tt = t.Where(it => ("," + it.TaskType + ",").Contains(",1,"));
        Count = tt.Count();
        WC = tt.Count(it => it.TaskStatus == (int)任务状态.已完成);
        ZC = tt.Count(it => it.TaskStatus == (int)任务状态.正常推进);
        YJ = tt.Count(it => it.TaskStatus == (int)任务状态.预警);
        ZH = tt.Count(it => it.TaskStatus == (int)任务状态.滞后);
        WCS = WC * WCV;
        ZCS = ZC * ZCV;
        YJS = YJ * YJV;
        ZHS = ZH * ZHV;
        AvgScore = Count == 0 ? 0 : (WCS + ZCS + YJS + ZHS) / Count;
        Score = Math.Round(AvgScore, 2);
        rank.Add(new Yqd_NewRank()
        {
            Year = year,
            Name = "金华市",
            UnitCode = "330700000000",
            FAreaCode = "330700000000",
            TaskType = "1",
            Score = Score,
            ResultTime = DateTime.Now.Date,
            Tactic = ctx.Database.MaxID("Yqd_NewRank", "Tactic")
        });
        //政府工作报告征求意见对接会
        ts = t.Where(it => ("," + it.TaskType + ",").Contains(",2,"));
        Count = ts.Count();
        WC = ts.Count(it => it.TaskStatus == (int)任务状态.已完成);
        ZC = ts.Count(it => it.TaskStatus == (int)任务状态.正常推进);
        YJ = ts.Count(it => it.TaskStatus == (int)任务状态.预警);
        ZH = ts.Count(it => it.TaskStatus == (int)任务状态.滞后);
        WCS = WC * WCV;
        ZCS = ZC * ZCV;
        YJS = YJ * YJV;
        ZHS = ZH * ZHV;
        AvgScore = Count == 0 ? 0 : (WCS + ZCS + YJS + ZHS) / Count;
        Score = Math.Round(AvgScore, 2);
        rank.Add(new Yqd_NewRank()
        {
            Year = year,
            Name = "金华市",
            UnitCode = "330700000000",
            FAreaCode = "330700000000",
            TaskType = "2",
            Score = Score,
            ResultTime = DateTime.Now.Date,
            Tactic = ctx.Database.MaxID("Yqd_NewRank", "Tactic")
        });

        //义乌市
        t = task.Where(it => it.FAreaCode.Trim() == "330782000000");
        Count = t.Count();
        //全部
        WC = t.Count(it => it.TaskStatus == (int)任务状态.已完成);
        ZC = t.Count(it => it.TaskStatus == (int)任务状态.正常推进);
        YJ = t.Count(it => it.TaskStatus == (int)任务状态.预警);
        ZH = t.Count(it => it.TaskStatus == (int)任务状态.滞后);
        WCS = WC * WCV;
        ZCS = ZC * ZCV;
        YJS = YJ * YJV;
        ZHS = ZH * ZHV;
        AvgScore = Count == 0 ? 0 : (WCS + ZCS + YJS + ZHS) / Count;
        Score = Math.Round(AvgScore, 2);
        rank.Add(new Yqd_NewRank()
        {
            Year = year,
            Name = "义乌市",
            UnitCode = "330782000000",
            FAreaCode = "330782000000",
            TaskType = "",
            Score = Score,
            ResultTime = DateTime.Now.Date,
            Tactic = ctx.Database.MaxID("Yqd_NewRank", "Tactic")
        });
        //政府工作报告
        tt = t.Where(it => ("," + it.TaskType + ",").Contains(",1,"));
        Count = tt.Count();
        WC = tt.Count(it => it.TaskStatus == (int)任务状态.已完成);
        ZC = tt.Count(it => it.TaskStatus == (int)任务状态.正常推进);
        YJ = tt.Count(it => it.TaskStatus == (int)任务状态.预警);
        ZH = tt.Count(it => it.TaskStatus == (int)任务状态.滞后);
        WCS = WC * WCV;
        ZCS = ZC * ZCV;
        YJS = YJ * YJV;
        ZHS = ZH * ZHV;
        AvgScore = Count == 0 ? 0 : (WCS + ZCS + YJS + ZHS) / Count;
        Score = Math.Round(AvgScore, 2);
        rank.Add(new Yqd_NewRank()
        {
            Year = year,
            Name = "义乌市",
            UnitCode = "330782000000",
            FAreaCode = "330782000000",
            TaskType = "1",
            Score = Score,
            ResultTime = DateTime.Now.Date,
            Tactic = ctx.Database.MaxID("Yqd_NewRank", "Tactic")
        });
        //政府工作报告征求意见对接会
        ts = t.Where(it => ("," + it.TaskType + ",").Contains(",2,"));
        Count = ts.Count();
        WC = ts.Count(it => it.TaskStatus == (int)任务状态.已完成);
        ZC = ts.Count(it => it.TaskStatus == (int)任务状态.正常推进);
        YJ = ts.Count(it => it.TaskStatus == (int)任务状态.预警);
        ZH = ts.Count(it => it.TaskStatus == (int)任务状态.滞后);
        WCS = WC * WCV;
        ZCS = ZC * ZCV;
        YJS = YJ * YJV;
        ZHS = ZH * ZHV;
        AvgScore = Count == 0 ? 0 : (WCS + ZCS + YJS + ZHS) / Count;
        Score = Math.Round(AvgScore, 2);
        rank.Add(new Yqd_NewRank()
        {
            Year = year,
            Name = "义乌市",
            UnitCode = "330782000000",
            FAreaCode = "330782000000",
            TaskType = "2",
            Score = Score,
            ResultTime = DateTime.Now.Date,
            Tactic = ctx.Database.MaxID("Yqd_NewRank", "Tactic")
        });

        //责任单位
        var unit = ctx.Yqd_Unit.Select(it => new
        {
            it.UnitCode,
            it.Name
        }).ToList();
        foreach (var u in unit)
        {
            //全部
            t = task.Where(it => it.LeaderUnitCode == u.UnitCode);
            Count = t.Count();
            if (Count == 0) continue;
            //全部
            WC = t.Count(it => it.TaskStatus == (int)任务状态.已完成);
            ZC = t.Count(it => it.TaskStatus == (int)任务状态.正常推进);
            YJ = t.Count(it => it.TaskStatus == (int)任务状态.预警);
            ZH = t.Count(it => it.TaskStatus == (int)任务状态.滞后);
            WCS = WC * WCV;
            ZCS = ZC * ZCV;
            YJS = YJ * YJV;
            ZHS = ZH * ZHV;
            AvgScore = Count == 0 ? 0 : (WCS + ZCS + YJS + ZHS) / Count;
            Score = Math.Round(AvgScore, 2);
            rank.Add(new Yqd_NewRank()
            {
                Year = year,
                Name = u.Name,
                UnitCode = u.UnitCode,
                FAreaCode = "",
                TaskType = "",
                Score = Score,
                ResultTime = DateTime.Now.Date,
                Tactic = ctx.Database.MaxID("Yqd_NewRank", "Tactic")
            });
            //政府工作报告
            tt = t.Where(it => ("," + it.TaskType + ",").Contains(",1,"));
            Count = tt.Count();
            WC = tt.Count(it => it.TaskStatus == (int)任务状态.已完成);
            ZC = tt.Count(it => it.TaskStatus == (int)任务状态.正常推进);
            YJ = tt.Count(it => it.TaskStatus == (int)任务状态.预警);
            ZH = tt.Count(it => it.TaskStatus == (int)任务状态.滞后);
            WCS = WC * WCV;
            ZCS = ZC * ZCV;
            YJS = YJ * YJV;
            ZHS = ZH * ZHV;
            AvgScore = Count == 0 ? 0 : (WCS + ZCS + YJS + ZHS) / Count;
            Score = Math.Round(AvgScore, 2);
            rank.Add(new Yqd_NewRank()
            {
                Year = year,
                Name = u.Name,
                UnitCode = u.UnitCode,
                FAreaCode = "",
                TaskType = "1",
                Score = Score,
                ResultTime = DateTime.Now.Date,
                Tactic = ctx.Database.MaxID("Yqd_NewRank", "Tactic")
            });
            //政府工作报告征求意见对接会
            ts = t.Where(it => ("," + it.TaskType + ",").Contains(",2,"));
            Count = ts.Count();
            WC = ts.Count(it => it.TaskStatus == (int)任务状态.已完成);
            ZC = ts.Count(it => it.TaskStatus == (int)任务状态.正常推进);
            YJ = ts.Count(it => it.TaskStatus == (int)任务状态.预警);
            ZH = ts.Count(it => it.TaskStatus == (int)任务状态.滞后);
            WCS = WC * WCV;
            ZCS = ZC * ZCV;
            YJS = YJ * YJV;
            ZHS = ZH * ZHV;
            AvgScore = Count == 0 ? 0 : (WCS + ZCS + YJS + ZHS) / Count;
            Score = Math.Round(AvgScore, 2);
            rank.Add(new Yqd_NewRank()
            {
                Year = year,
                Name = u.Name,
                UnitCode = u.UnitCode,
                FAreaCode = "",
                TaskType = "2",
                Score = Score,
                ResultTime = DateTime.Now.Date,
                Tactic = ctx.Database.MaxID("Yqd_NewRank", "Tactic")
            });
            //浙江省
            t = t.Where(it => it.FAreaCode.Trim() == "330000000000");
            Count = t.Count();
            //全部
            WC = t.Count(it => it.TaskStatus == (int)任务状态.已完成);
            ZC = t.Count(it => it.TaskStatus == (int)任务状态.正常推进);
            YJ = t.Count(it => it.TaskStatus == (int)任务状态.预警);
            ZH = t.Count(it => it.TaskStatus == (int)任务状态.滞后);
            WCS = WC * WCV;
            ZCS = ZC * ZCV;
            YJS = YJ * YJV;
            ZHS = ZH * ZHV;
            AvgScore = Count == 0 ? 0 : (WCS + ZCS + YJS + ZHS) / Count;
            Score = Math.Round(AvgScore, 2);
            rank.Add(new Yqd_NewRank()
            {
                Year = year,
                Name = u.Name,
                UnitCode = u.UnitCode,
                FAreaCode = "330000000000",
                TaskType = "",
                Score = Score,
                ResultTime = DateTime.Now.Date,
                Tactic = ctx.Database.MaxID("Yqd_NewRank", "Tactic")
            });
            //政府工作报告
            tt = t.Where(it => ("," + it.TaskType + ",").Contains(",1,"));
            Count = tt.Count();
            WC = tt.Count(it => it.TaskStatus == (int)任务状态.已完成);
            ZC = tt.Count(it => it.TaskStatus == (int)任务状态.正常推进);
            YJ = tt.Count(it => it.TaskStatus == (int)任务状态.预警);
            ZH = tt.Count(it => it.TaskStatus == (int)任务状态.滞后);
            WCS = WC * WCV;
            ZCS = ZC * ZCV;
            YJS = YJ * YJV;
            ZHS = ZH * ZHV;
            AvgScore = Count == 0 ? 0 : (WCS + ZCS + YJS + ZHS) / Count;
            Score = Math.Round(AvgScore, 2);
            rank.Add(new Yqd_NewRank()
            {
                Year = year,
                Name = u.Name,
                UnitCode = u.UnitCode,
                FAreaCode = "330000000000",
                TaskType = "1",
                Score = Score,
                ResultTime = DateTime.Now.Date,
                Tactic = ctx.Database.MaxID("Yqd_NewRank", "Tactic")
            });
            //政府工作报告征求意见对接会
            ts = t.Where(it => ("," + it.TaskType + ",").Contains(",2,"));
            Count = ts.Count();
            WC = ts.Count(it => it.TaskStatus == (int)任务状态.已完成);
            ZC = ts.Count(it => it.TaskStatus == (int)任务状态.正常推进);
            YJ = ts.Count(it => it.TaskStatus == (int)任务状态.预警);
            ZH = ts.Count(it => it.TaskStatus == (int)任务状态.滞后);
            WCS = WC * WCV;
            ZCS = ZC * ZCV;
            YJS = YJ * YJV;
            ZHS = ZH * ZHV;
            AvgScore = Count == 0 ? 0 : (WCS + ZCS + YJS + ZHS) / Count;
            Score = Math.Round(AvgScore, 2);
            rank.Add(new Yqd_NewRank()
            {
                Year = year,
                Name = u.Name,
                UnitCode = u.UnitCode,
                FAreaCode = "330000000000",
                TaskType = "2",
                Score = Score,
                ResultTime = DateTime.Now.Date,
                Tactic = ctx.Database.MaxID("Yqd_NewRank", "Tactic")
            });

            //金华市
            t = task.Where(it => it.FAreaCode.Trim() == "330700000000");
            Count = t.Count();
            //全部
            WC = t.Count(it => it.TaskStatus == (int)任务状态.已完成);
            ZC = t.Count(it => it.TaskStatus == (int)任务状态.正常推进);
            YJ = t.Count(it => it.TaskStatus == (int)任务状态.预警);
            ZH = t.Count(it => it.TaskStatus == (int)任务状态.滞后);
            WCS = WC * WCV;
            ZCS = ZC * ZCV;
            YJS = YJ * YJV;
            ZHS = ZH * ZHV;
            AvgScore = Count == 0 ? 0 : (WCS + ZCS + YJS + ZHS) / Count;
            Score = Math.Round(AvgScore, 2);
            rank.Add(new Yqd_NewRank()
            {
                Year = year,
                Name = u.Name,
                UnitCode = u.UnitCode,
                FAreaCode = "330700000000",
                TaskType = "",
                Score = Score,
                ResultTime = DateTime.Now.Date,
                Tactic = ctx.Database.MaxID("Yqd_NewRank", "Tactic")
            });
            //政府工作报告
            tt = t.Where(it => ("," + it.TaskType + ",").Contains(",1,"));
            Count = tt.Count();
            WC = tt.Count(it => it.TaskStatus == (int)任务状态.已完成);
            ZC = tt.Count(it => it.TaskStatus == (int)任务状态.正常推进);
            YJ = tt.Count(it => it.TaskStatus == (int)任务状态.预警);
            ZH = tt.Count(it => it.TaskStatus == (int)任务状态.滞后);
            WCS = WC * WCV;
            ZCS = ZC * ZCV;
            YJS = YJ * YJV;
            ZHS = ZH * ZHV;
            AvgScore = Count == 0 ? 0 : (WCS + ZCS + YJS + ZHS) / Count;
            Score = Math.Round(AvgScore, 2);
            rank.Add(new Yqd_NewRank()
            {
                Year = year,
                Name = u.Name,
                UnitCode = u.UnitCode,
                FAreaCode = "330700000000",
                TaskType = "1",
                Score = Score,
                ResultTime = DateTime.Now.Date,
                Tactic = ctx.Database.MaxID("Yqd_NewRank", "Tactic")
            });
            //政府工作报告征求意见对接会
            ts = t.Where(it => ("," + it.TaskType + ",").Contains(",2,"));
            Count = ts.Count();
            WC = ts.Count(it => it.TaskStatus == (int)任务状态.已完成);
            ZC = ts.Count(it => it.TaskStatus == (int)任务状态.正常推进);
            YJ = ts.Count(it => it.TaskStatus == (int)任务状态.预警);
            ZH = ts.Count(it => it.TaskStatus == (int)任务状态.滞后);
            WCS = WC * WCV;
            ZCS = ZC * ZCV;
            YJS = YJ * YJV;
            ZHS = ZH * ZHV;
            AvgScore = Count == 0 ? 0 : (WCS + ZCS + YJS + ZHS) / Count;
            Score = Math.Round(AvgScore, 2);
            rank.Add(new Yqd_NewRank()
            {
                Year = year,
                Name = u.Name,
                UnitCode = u.UnitCode,
                FAreaCode = "330700000000",
                TaskType = "2",
                Score = Score,
                ResultTime = DateTime.Now.Date,
                Tactic = ctx.Database.MaxID("Yqd_NewRank", "Tactic")
            });

            //义乌市
            t = task.Where(it => it.FAreaCode.Trim() == "330782000000");
            Count = t.Count();
            //全部
            WC = t.Count(it => it.TaskStatus == (int)任务状态.已完成);
            ZC = t.Count(it => it.TaskStatus == (int)任务状态.正常推进);
            YJ = t.Count(it => it.TaskStatus == (int)任务状态.预警);
            ZH = t.Count(it => it.TaskStatus == (int)任务状态.滞后);
            WCS = WC * WCV;
            ZCS = ZC * ZCV;
            YJS = YJ * YJV;
            ZHS = ZH * ZHV;
            AvgScore = Count == 0 ? 0 : (WCS + ZCS + YJS + ZHS) / Count;
            Score = Math.Round(AvgScore, 2);
            rank.Add(new Yqd_NewRank()
            {
                Year = year,
                Name = u.Name,
                UnitCode = u.UnitCode,
                FAreaCode = "330782000000",
                TaskType = "",
                Score = Score,
                ResultTime = DateTime.Now.Date,
                Tactic = ctx.Database.MaxID("Yqd_NewRank", "Tactic")
            });
            //政府工作报告
            tt = t.Where(it => ("," + it.TaskType + ",").Contains(",1,"));
            Count = tt.Count();
            WC = tt.Count(it => it.TaskStatus == (int)任务状态.已完成);
            ZC = tt.Count(it => it.TaskStatus == (int)任务状态.正常推进);
            YJ = tt.Count(it => it.TaskStatus == (int)任务状态.预警);
            ZH = tt.Count(it => it.TaskStatus == (int)任务状态.滞后);
            WCS = WC * WCV;
            ZCS = ZC * ZCV;
            YJS = YJ * YJV;
            ZHS = ZH * ZHV;
            AvgScore = Count == 0 ? 0 : (WCS + ZCS + YJS + ZHS) / Count;
            Score = Math.Round(AvgScore, 2);
            rank.Add(new Yqd_NewRank()
            {
                Year = year,
                Name = u.Name,
                UnitCode = u.UnitCode,
                FAreaCode = "330782000000",
                TaskType = "1",
                Score = Score,
                ResultTime = DateTime.Now.Date,
                Tactic = ctx.Database.MaxID("Yqd_NewRank", "Tactic")
            });
            //政府工作报告征求意见对接会
            ts = t.Where(it => ("," + it.TaskType + ",").Contains(",2,"));
            Count = ts.Count();
            WC = ts.Count(it => it.TaskStatus == (int)任务状态.已完成);
            ZC = ts.Count(it => it.TaskStatus == (int)任务状态.正常推进);
            YJ = ts.Count(it => it.TaskStatus == (int)任务状态.预警);
            ZH = ts.Count(it => it.TaskStatus == (int)任务状态.滞后);
            WCS = WC * WCV;
            ZCS = ZC * ZCV;
            YJS = YJ * YJV;
            ZHS = ZH * ZHV;
            AvgScore = Count == 0 ? 0 : (WCS + ZCS + YJS + ZHS) / Count;
            Score = Math.Round(AvgScore, 2);
            rank.Add(new Yqd_NewRank()
            {
                Year = year,
                Name = u.Name,
                UnitCode = u.UnitCode,
                FAreaCode = "330782000000",
                TaskType = "2",
                Score = Score,
                ResultTime = DateTime.Now.Date,
                Tactic = ctx.Database.MaxID("Yqd_NewRank", "Tactic")
            });
        }
        foreach (var r in rank)
        {
            var rn = ctx.Yqd_NewRank.FirstOrDefault(it => it.Name == r.Name && it.UnitCode == r.UnitCode && it.Year == r.Year && it.FAreaCode == r.FAreaCode && it.TaskType == r.TaskType && it.ResultTime == r.ResultTime);
            if (rn != null) rn.Score = r.Score;
            else ctx.Yqd_NewRank.Add(r);
        }
        var ret = ctx.SaveChanges();
        Logs.Warning<RankService>("部门效能开始更新！已更新" + ret + "条。");
    }
}
    }
}